<?php

/*
 Website Baker Project <http://www.websitebaker.org/>
 Copyright (C) 2004-2007, Ryan Djurovich

 Website Baker is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 Website Baker is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Website Baker; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// Must include code to stop this file being access directly
if(defined('WB_PATH') == false) { exit("Cannot access this file directly"); }

// Get settings
$query_settings = $database->query("SELECT * FROM `".TABLE_PREFIX."mod_map_settings` WHERE section_id='$section_id' LIMIT 1");
$fetch_settings = $query_settings->fetchRow();

// gmaps configuration settings
$gmkey = $admin->strip_slashes($fetch_settings['gmkey']);
$maptype = $admin->strip_slashes($fetch_settings['maptype']);
$deflatitude = $admin->strip_slashes($fetch_settings['deflatitude']);
$deflongitude = $admin->strip_slashes($fetch_settings['deflongitude']);
$defzoom = $admin->strip_slashes($fetch_settings['defzoom']);
$template_infowindow = $admin->strip_slashes($fetch_settings['template_infowindow']);
$mapwidth = $admin->strip_slashes($fetch_settings['width']);
$mapheight = $admin->strip_slashes($fetch_settings['height']);

//build template
echo '<script type="text/javascript" src="http://www.google.com/jsapi?key='.$gmkey.'"></script>
<script type="text/javascript">

	var map;
	google.load("maps", "2");
	
	function buildIcon(width,iconUrl,shdUrl){
		var ico = new GIcon(G_DEFAULT_ICON);
		
		var height = parseInt(calcIconHeight(width,iconUrl));

		var icoSize = new GSize(width,height);
		var shdSize = new GSize(width*2,height);

		ico.image = iconUrl;
		ico.shadow = shdUrl;
		ico.iconSize =  icoSize;
		ico.shadowSize = shdSize;

		ico.iconAnchor = new GPoint(width/2,height);
		ico.infoWindowAnchor = new GPoint(0, height);

		return ico;
	} 		
	
	function calcIconHeight(width,url){
		var img = new Image();
		img.src = url;
		var height = parseInt(img.naturalHeight)*parseInt(width)/parseInt(img.naturalWidth);
		return height;
	} 		

	function createMarker(lat,lng,desc,markerOptions) {
		var latlng = new GLatLng(lat,lng);
		var marker = new GMarker(latlng,markerOptions);
		marker.value = desc;
		GEvent.addListener(marker,"click", function() {
			var myHtml = desc;
			map.openInfoWindowHtml(latlng, myHtml);
		});
		return marker;
	}

	function initialize()
	{
		map = new google.maps.Map2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setMapType('.$maptype.');	
		map.setCenter(new google.maps.LatLng('.$deflatitude.', '.$deflongitude.'), '.$defzoom.');
		map.enableScrollWheelZoom();

		';
		
		// Add geofeeds
		$query_feeds = $database->query("SELECT * FROM `".TABLE_PREFIX."mod_map_geofeeds` WHERE section_id='".$section_id."' ORDER BY pos ASC");
		if($query_feeds->numRows() > 0)
		{
			$feedcount = 0;
			while($feed = $query_feeds->fetchRow())
			{
				$url=$admin->strip_slashes($feed['url']);
				$active=$admin->strip_slashes($feed['active']);
				if ($active == '1')
				{			
					echo '
		var g'.$feedcount.' = new GGeoXml("'.$url.'");
		map.addOverlay(g'.$feedcount.');
					';
				}
				$feedcount++;
			}		
		}

		// Add markers			
		$query_cats = $database->query("SELECT "
		." ".TABLE_PREFIX."mod_map_categories.section_id, "
		." ".TABLE_PREFIX."mod_map_categories.page_id, "
		." ".TABLE_PREFIX."mod_map_categories.cat_id, "
		." ".TABLE_PREFIX."mod_map_categories.cat_name, "
		." ".TABLE_PREFIX."mod_map_categories.icon_id, "
		." ".TABLE_PREFIX."mod_map_categories.active, "
		." ".TABLE_PREFIX."mod_map_categories.pos, "
		." ".TABLE_PREFIX."mod_map_icons.icon, "
		." ".TABLE_PREFIX."mod_map_icons.shaddow "
		." FROM ".TABLE_PREFIX."mod_map_categories "
		." LEFT JOIN ".TABLE_PREFIX."mod_map_icons "
		." ON ".TABLE_PREFIX."mod_map_categories.icon_id=".TABLE_PREFIX."mod_map_icons.icon_id "
		." ORDER BY ".TABLE_PREFIX."mod_map_categories.pos ASC");
				
		if($query_cats->numRows() > 0)
		{		
			while($cat = $query_cats->fetchRow())
			{
				$activeCat=$admin->strip_slashes($cat['active']);
				if($activeCat==1)
				{
					if($cat['icon_id']=='default')
						echo '
		var cat'.$cat['cat_id'].'Icon = new GIcon(G_DEFAULT_ICON);
		markerOptions = { icon:cat'.$cat['cat_id'].'Icon };
';
					else
						echo '
		var cat'.$cat['cat_id'].'Icon = buildIcon(20,\''.$cat['icon'].'\',\''.$cat['shaddow'].'\');
		markerOptions = { icon:cat'.$cat['cat_id'].'Icon };
';


					$query_pins = $database->query("SELECT * FROM `".TABLE_PREFIX."mod_map_markers` WHERE cat_id='".$cat['cat_id']."' and section_id='".$section_id."' ORDER BY pos ASC");
					if($query_pins->numRows() > 0)
					{
						while($pin = $query_pins->fetchRow())
						{
							$active=$admin->strip_slashes($pin['active']);
							
							if($active=='1')
							{
								$marker=$admin->strip_slashes($pin['marker']);
								$latitude=$admin->strip_slashes($pin['latitude']);
								$longitude=$admin->strip_slashes($pin['longitude']);
								$description=$admin->strip_slashes($pin['description']);
								$marker_id=$admin->strip_slashes($pin['marker_id']);
								
								//template						
								$replace_pattern = array('{NAME}' => $marker, '{DESCRIPTION}'  => $description, '{LATITUDE}'  => $latitude, '{LONGITUDE}'  => $longitude);
								$processedDescription = strtr($template_infowindow, $replace_pattern);
								
								$processedDescription = str_replace(array("\r\n","\n","\r","\t"),"",$processedDescription);
								$processedDescription = str_replace(array("'"),"\'",$processedDescription);
								
								echo '
		var marker'.$marker_id.' = createMarker('.$latitude.','.$longitude.',\''.$processedDescription.'\',markerOptions);
		map.addOverlay(marker'.$marker_id.');
';
							}
						}
					}
				}
			}
		}
		
		//highlight marker by querystring marker_id
		if(isset($_GET["marker_id"]) AND is_numeric($_GET["marker_id"]))
		{
			$marker_id = $_GET['marker_id'];
			echo '
			if(typeof(marker'.$marker_id .')!="undefined")
				GEvent.trigger(marker'.$marker_id .', "click");
			';
		}
		
		echo'
      }
      google.setOnLoadCallback(initialize);
    </script>
	';

// Add a top anchor
echo '<a name="top"></a>';
// map div
echo '<div id="map" onmousewheel="cancelscroll()" style="width: '.$mapwidth.'px; height: '.$mapheight.'px; border: 1px solid #000000;"></div>';


?>
